import pygame
import sys
class Bird(object):
    def __init__(self):
        self.birdRect = pygame.Rect(65, 50, 50, 50)
        self.birdStatus = [
            pygame.image.load("assets/1.png"),
            pygame.image.load("assets/2.png"),
            pygame.image.load("assets/dead.png")
        ]
        self.status = 0
        self.birdX = 120
        self.birdY = 350
        self.jump = False
        self.jumpSpeed = 10
        self.gravity = 5
        self.dead = False

    def birdUpdate(self):
        if self.jump:
            self.jumpSpeed -= 1
            self.birdY -= self.jumpSpeed
        else:
            self.gravity += 0.2
            self.birdY += self.gravity
        self.birdRect[1] = self.birdY       # 更改Y轴位置
        
class Pipeline(object):
    def __init__(self):
        self.wallx = 400
        self.pipeUp = pygame.image.load("assets/top.png")
        self.pipeDown = pygame.image.load("assets/bottom.png")

    def updatePipeline(self):
        self.wallx -= 5
        if self.wallx < -80:
            global score
            score += 1
            self.wallx = 400

def checkDead():
    # 上管道的矩形位置
    upRect = pygame.Rect(
        Pipeline.wallx, -300, Pipeline.pipeUp.get_width() - 10, Pipeline.pipeUp.get_height())
    # 下管道的矩形位置
    downRect = pygame.Rect(
        Pipeline.wallx, -300, Pipeline.pipeDown.get_width() - 10, Pipeline.pipeDown.get_height())

    # 检测小鸟上下方管道是否相撞
    if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect):
        Bird.dead = True

    # 检测小鸟是否飞出上下边界
    print(Bird.birdRect[1], height)
    if not 0 < Bird.birdRect[1] < height:
        Bird.dead = True
        return True
    else:
        return False

def getResult():
    final_text1 = "Game Over"
    final_text2 = "Your final score is: " + str(score)
    ft1_font = pygame.font.SysFont("Arial", 70)
    ft1_surf = font.render(final_text1, 1, (242, 3, 36))
    ft2_font = pygame.font.SysFont("Arial", 50)
    ft2_surf = font.render(final_text2, 1, (253, 177, 6))
    # 设置第一行字体的显示位置
    screen.blit(ft1_surf, [screen.get_width()/2-ft1_surf.get_width()/2, 100])
    screen.blit(ft2_surf, [screen.get_width()/2-ft2_surf.get_width()/2, 200])
    pygame.display.flip()   # 更新整个待显示的Surface对象到屏幕上


def createMap():
    color = (255, 255, 255)
    screen.fill(color)
    screen.blit(background, (0, 0))

    # 显示管道
    screen.blit(Pipeline.pipeUp, (Pipeline.wallx, -300))
    screen.blit(Pipeline.pipeDown, (Pipeline.wallx, 500))
    Pipeline.updatePipeline()

    # 显示小鸟
    if Bird.dead:
        Bird.status = 2
    elif Bird.jump:
        Bird.status = 1

    screen.blit(Bird.birdStatus[Bird.status], (Bird.birdX, Bird.birdY))
    Bird.birdUpdate()

    screen.blit(font.render('Score:' + str(score), -
                1, (255, 255, 255)), (100, 50))
    pygame.display.update()

if __name__ == '__main__':
    """主程序"""
    pygame.init()   # 初始化pygame

    pygame.font.init()
    font = pygame.font.SysFont(None, 50)

    size = width, height = 400, 650
    screen = pygame.display.set_mode(size)  # 设置窗口
    clock = pygame.time.Clock()
    Bird = Bird()  # 实例化小鸟类
    Pipeline = Pipeline()  # 实例化管道类
    score = 0

    while True:
        clock.tick(60)

        # 轮询事件监测
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if (event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN) and not Bird.dead:
                Bird.jump = True
                Bird.gravity = 5
                Bird.jumpSpeed = 10

        background = pygame.image.load("assets/background.png")   # 加载背景图片
        if checkDead():
            getResult()

        else:
            createMap()   # 生成地图
